package com.zhuiYun.project.api.user.controller;

import com.zhuiYun.project.api.user.model.UserModel;
import com.zhuiYun.project.api.user.service.UserService;
import com.zhuiYun.project.common.controller.BaseController;
import com.zhuiYun.project.common.errorCode.CommonException;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.pageUtil.PageRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

/**
 * @ClassName UserController
 * @Description TODO 用户信息修改
 * @Author Root
 * @Date 2023/4/4 10:47
 **/
@RestController
@Api(tags = "用户信息管理")
@RequestMapping("/upUser")
@Slf4j
@CrossOrigin
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
    * @Description 获取建立会话的用户信息列表
    * @Author lzy
    * @Date 2023/4/21 10:42
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param userIds
    */
    @GetMapping("/getUserListByUserIds")
    public CommonResult getUserListByUserIds(String userIds) {
        return userService.getUserListByUserIds(userIds);
    }

    /**
    * @Author Root
    * @Description  修改用户信息
    * @Date 2023/4/4 11:12
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param userModel
    */
    @PostMapping("/updateUser")
    @ApiOperation(value = "用户管理的修改方法")
    public CommonResult updateUser(MultipartRequest imageFile, UserModel userModel) throws CommonException {
        //接受头像文件存储
        MultipartFile file = imageFile.getFile("file");
        //调用修改方法
        Integer integer = userService.updateUser(file, userModel);
        if(integer>0){
            return CommonResult.ok();
        }else{
            return CommonResult.error("修改失败");
        }
    }
    /**
    * @Author Root
    * @Description 用户查询
    * @Date 2023/4/5 9:03
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param id
    */
    @GetMapping("/getUserById")
    @ApiOperation(value = "用户id查询")
    public CommonResult getUserById( Integer id){
        //查询
        return userService.getUserById(id);
    }
    /**
    * @Author Root
    * @Description pc用户查找密码
    * @Date 2023/4/20 10:30
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param id
* @param password
    */
    @GetMapping("/selectPassword")
    public CommonResult selectPassword(Integer id, String password){
        return userService.selectPassword(id,password);
    }
    /**
    * @Author Root
    * @Description  用户信息新增
    * @Date 2023/4/6 18:37
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param imageFile
    * @param userModel
    */
    @PostMapping("/saveUser")
    @ApiOperation(value = "新增用户信息的方法")
    public CommonResult saveUser(MultipartRequest imageFile, UserModel userModel){
        //接受用户头像的存储
        MultipartFile file = imageFile.getFile("file");
        Integer i = userService.saveUser(file, userModel);
        if(i>0){
            return CommonResult.ok();
        }else{
            return CommonResult.error("修改失败");
        }
    }
    /**
    * @Author Root
    * @Description  删除用户信息
    * @Date 2023/4/7 8:35
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param id
    */
    @DeleteMapping("/deleteUser")
    @ApiOperation(value = "删除用户信息的方法")
    public CommonResult deleteUser(Integer id){
        return userService.deleteUser(id);
    }

    /**
    * @Author Root
    * @Description 分页查询
    * @Date 2023/4/12 8:50
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param userModel
    * @param pageRequest
    */
    @GetMapping("/getUserByPage")
    @ApiOperation("分页查询")
    public CommonResult getUserByPage(UserModel userModel, PageRequest pageRequest){
        return userService.getUserByPage(userModel,pageRequest);
    }
    /**
    * @Author Root
    * @Description pc端用户修改密码
    * @Date 2023/4/18 15:03
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param userModel
    */
    @PostMapping("/updatePassword")
    public CommonResult updatePassword(UserModel userModel){

        Integer i = userService.updatePassword(userModel);
        if(i>0){
            return CommonResult.ok();
        }
        return CommonResult.error();
    }
    /**
    * @Author Root
    * @Description pc端修改邮箱
    * @Date 2023/4/18 15:04
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param userModel
    */
    @PostMapping("/updateMail")
    public CommonResult updateMail(UserModel userModel){
        Integer i = userService.updateMail(userModel);
        if(i>0){
            return CommonResult.ok();
        }
        return CommonResult.error();
    }
    /**
    * @Author Root
    * @Description pc端修改用户用户名
    * @Date 2023/4/18 16:35
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param userModel
    */
    @PostMapping("/updateUserName")
    public CommonResult updateUserName(UserModel userModel){
        Integer i = userService.updateUserName(userModel);
        if(i>0){
            return CommonResult.ok();
        }
        return CommonResult.error();
    }
    /**
    * @Author Root
    * @Description pc端修改用户手机号
    * @Date 2023/4/18 18:34
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param userModel
    */
    @PostMapping("/updatePhone")
    public CommonResult updatePhone(UserModel userModel){
        Integer i = userService.updatePhone(userModel);
        if(i>0){
            return CommonResult.ok();
        }
        return CommonResult.error();
    }

    /**
    * @Author Root
    * @Description pc端邮箱发送验证码
    * @Date 2023/4/18 15:04
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param mail
    */
    @GetMapping("/registerByMailCode")
    @ApiOperation(value = "pc端修改邮箱向邮箱发送验证码")
    public CommonResult registerByMailCode(String mail){
        return CommonResult.ok(userService.registerByMailCode(mail));
    }
    /**
    * @Author Root
    * @Description pc端校验邮箱的验证码
    * @Date 2023/4/18 15:04
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param mail
    * @param code
    */
    @GetMapping("/determineMailCode")
    @ApiOperation(value = "pc端修改邮箱校验邮箱验证码是否正确")
    public CommonResult determineMailCode(String mail,String code){
        return userService.determineMailCode(mail,code);
    }
    /**
    * @Author Root
    * @Description pc查询邮箱是否重复
    * @Date 2023/4/18 15:05
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param mail
    */
    @GetMapping("/checkMail")
    @ApiOperation(value = "pc端修改检查邮箱是否重复")
    public CommonResult checkMail(String mail){
        return CommonResult.ok(userService.checkMail(mail));
    }
    /**
    * @Author Root
    * @Description pc端检查电话是否重复
    * @Date 2023/4/18 15:05
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param phone
    */
    @GetMapping("/checkPhone")
    @ApiOperation(value = "pc端修改检查电话是否重复")
    public CommonResult checkPhone(String phone){
        return CommonResult.ok(userService.checkPhone(phone));
    }
    /**
    * @Author Root
    * @Description pc检查用户名是否存在
    * @Date 2023/4/18 16:20
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param userName
    */
    @GetMapping("/checkUserName")
    @ApiOperation(value = "pc端修改检查用户名是否重复")
    public CommonResult checkUserName(String userName){
        return CommonResult.ok(userService.checkUserName(userName));
    }
    
    /**
    * @Author Root
    * @Description pc修改手机号时发送验证码
    * @Date 2023/4/19 10:16
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param phone
    */
    @GetMapping("/sendPhoneCode")
    public CommonResult sendPhoneCode(String phone){
        return userService.sendPhoneCode(phone);
    }
    /**
    * @Author Root
    * @Description pc端检验手机号验证码是否正确
    * @Date 2023/4/19 10:17
    * @Return com.zhuiYun.project.common.response.CommonResult
    * @param code
    */
    @GetMapping("/comparePhoneCode")
    public CommonResult comparePhoneCode(String code){
        return CommonResult.ok(userService.comparePhoneCode(code));
    }
}

